/**
 * 
 */
package edu.uwm.nlp.jude.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


import abner.Tagger;

/**
 * @author qing
 *
 * Jan 16, 2009
 */
public class CRFDecoder {
	
//	private final String model = "Z:/citationdata/citationtrain9.crf";
//	private final String model = "c:/faraday/crfmodel/citationtrain9.crf";
//	private final String model = "/home/qing/fireremote/cithit/citationtrain9.crf";
	private String model = "/home/data_user/citationdata/citationtrain9.crf";
	private Tagger t;
	public CRFDecoder()
	{
		t = new Tagger(model);
	}
	
	public CRFDecoder(String model)
	{
		this.model = model;
		t = new Tagger(model);
	}
	
	public ArrayList tagField(String fulcit)
	{
		
		ArrayList tagRes = new ArrayList();
		Vector vec = t.tagString(fulcit);
		StringBuffer fullform = new StringBuffer();
		StringBuffer field = null;
		ArrayList totalResult = new ArrayList();
		for (int i = 0; i < vec.size(); i++) {
			ArrayList<Field> fields = new ArrayList<Field>();
			String[][] strs = (String[][]) vec.get(i);
		    
			for (int j = 0; j < strs[0].length; j++) {
				
				String tag = strs[1][j];
				if (tag.startsWith("B-")) {
					field = addField(fields, tag);
					
					
				}
				if (!tag.equals("O")) {
					if (field == null) {// start with no beginning
						field = addField(fields, tag);
					}
					if (field.length() > 0
							&& Character.isLetter(field.charAt(field
									.length() - 1))
							&& Character.isLetter(strs[0][j].charAt(0))) {
						field.append(' ');
					}
					field.append(strs[0][j]);
					
				}

				if (fullform.length() > 0
						&& Character.isLetter(fullform.charAt(fullform
								.length() - 1))
						&& Character.isLetter(strs[0][j].charAt(0))) {
					fullform.append(' ');
				}

				fullform.append(strs[0][j]);
				//					

			}

			System.out.println(fullform);
			int ii = 0;
			StringBuffer strauthor=new StringBuffer();
			ArrayList sglResult = new ArrayList();
			for (Field s : fields) {
				if (s.value.length() > 10) 
				{
					System.out.println(s.toString());
					ArrayList cell = new ArrayList();
					cell.add(s.name);
					cell.add(s.value.toString());
					sglResult.add(cell);
				} 
				else 
				{
					if(s.name.equals("SN")||s.name.equals("GN")){
						if(strauthor.length()>0&&s.name.equals("SN")){
							ArrayList cell = new ArrayList();
							cell.add("AUTHOR");
							cell.add(strauthor.toString());
							sglResult.add(cell);
							System.out.println("AUTHOR("+strauthor+")");
							strauthor.delete(0, strauthor.length());
							
						}
						strauthor.append(s.toString()+"\t");
						continue;
					}else{
						if(strauthor.length()>0){
							ArrayList cell = new ArrayList();
							cell.add("AUTHOR");
							cell.add(strauthor.toString());
							sglResult.add(cell);
							System.out.println("AUTHOR("+strauthor+")");
							strauthor.delete(0, strauthor.length());
							
						}
					}
					ArrayList cell = new ArrayList();
					cell.add(s.name);
					cell.add(s.value.toString());
					sglResult.add(cell);
					System.out.print(s.toString() + "\t");
					ii++;
					if (ii % 4 == 0)
						System.out.println();
				}

			}
			System.out.println();
			totalResult.add(sglResult);
		}
		return totalResult;
	}
	
	private static StringBuffer addField(ArrayList<Field> fields, String tag) {
		StringBuffer field;
		Field f = new Field(tag.substring(2));
		fields.add(f);
		field = f.value;
		return field;
	}
	
	public static class Field {
		public String name;
		public StringBuffer value;

		public Field(String n) {
			name = n;
			value = new StringBuffer();
		}

		public String toString() {
			return name + ":" + value.toString();
		}

	}
	

}
